home *** CD-ROM | disk | FTP | other *** search
/ Champak 141 / (Vol 141) Oct 17 2011.iso / Games / Clueless.swf / scripts / PartTemplatePackage.as < prev    next >
Encoding:
Text File  |  2011-10-17  |  20.9 KB  |  601 lines

  1. package
  2. {
  3.    import Common.ResourceManager;
  4.    import flash.display.BitmapData;
  5.    import flash.geom.Matrix;
  6.    import flash.geom.Point;
  7.    import flash.geom.Rectangle;
  8.    import flash.utils.getDefinitionByName;
  9.    
  10.    public class PartTemplatePackage
  11.    {
  12.       
  13.       internal static const SHOE_SCALE_CUTTOFF:int = 150;
  14.       
  15.       internal static const BOUNDARY_SCALE:Number = 0.4375;
  16.        
  17.       
  18.       internal var _fnCallback:Function;
  19.       
  20.       public var Rules:Array;
  21.       
  22.       public var Templates:Array;
  23.       
  24.       internal var _sPackageFilename:String;
  25.       
  26.       public var Poses:Array;
  27.       
  28.       internal var _xmlresmanager:ResourceManager;
  29.       
  30.       internal var _resmanager:ResourceManager;
  31.       
  32.       public function PartTemplatePackage()
  33.       {
  34.          Templates = new Array();
  35.          Poses = new Array();
  36.          Rules = new Array();
  37.          _xmlresmanager = new ResourceManager();
  38.          _resmanager = new ResourceManager();
  39.          super();
  40.       }
  41.       
  42.       public function loadFile(param1:String, param2:Function) : void
  43.       {
  44.          _sPackageFilename = param1;
  45.          _fnCallback = param2;
  46.          _xmlresmanager.CallbackFunction = onXMLLoaded;
  47.          _resmanager.CallbackFunction = onResLoaded;
  48.          _xmlresmanager.queueResourceLoad(_sPackageFilename,"Package",ResourceManager.C_XML);
  49.          _xmlresmanager.loadResource();
  50.       }
  51.       
  52.       public function getMatchList(param1:PartTemplate) : Array
  53.       {
  54.          var _loc2_:Array = null;
  55.          var _loc3_:Array = null;
  56.          var _loc4_:String = null;
  57.          var _loc5_:PartTemplate = null;
  58.          var _loc6_:Object = null;
  59.          _loc2_ = new Array();
  60.          _loc3_ = new Array();
  61.          _loc4_ = param1.Category;
  62.          for each(_loc5_ in Templates)
  63.          {
  64.             if(_loc5_.Category == _loc4_ && _loc5_ != param1)
  65.             {
  66.                (_loc6_ = new Object()).Item = _loc5_;
  67.                _loc6_.MatchScore = param1.getNumMatch(_loc5_);
  68.                _loc3_.push(_loc6_);
  69.             }
  70.          }
  71.          _loc3_.sortOn("MatchScore",Array.DESCENDING);
  72.          for each(_loc6_ in _loc3_)
  73.          {
  74.             _loc2_.push(_loc6_.Item);
  75.          }
  76.          return _loc2_;
  77.       }
  78.       
  79.       public function getAllTemplates(param1:String) : Array
  80.       {
  81.          var _loc2_:Array = null;
  82.          var _loc3_:int = 0;
  83.          var _loc4_:PartTemplate = null;
  84.          _loc2_ = new Array();
  85.          for each(_loc4_ in Templates)
  86.          {
  87.             if(_loc4_.Category == param1)
  88.             {
  89.                _loc2_.push(_loc4_);
  90.             }
  91.          }
  92.          return _loc2_;
  93.       }
  94.       
  95.       public function getMultipleRandomTemplates(param1:String, param2:int) : Array
  96.       {
  97.          var _loc3_:Array = null;
  98.          var _loc4_:int = 0;
  99.          var _loc5_:PartTemplate = null;
  100.          var _loc6_:Number = NaN;
  101.          _loc3_ = new Array();
  102.          for each(_loc5_ in Templates)
  103.          {
  104.             if(_loc5_.Category == param1)
  105.             {
  106.                _loc3_.push(_loc5_);
  107.             }
  108.             else if(_loc5_.Category == "dress" && param1 == "top")
  109.             {
  110.                _loc3_.push(_loc5_);
  111.             }
  112.          }
  113.          DressupLevelInfo.shuffleArray(_loc3_);
  114.          return _loc3_.slice(0,param2);
  115.       }
  116.       
  117.       public function getTemplateByName(param1:String) : PartTemplate
  118.       {
  119.          var _loc2_:PartTemplate = null;
  120.          for each(_loc2_ in Templates)
  121.          {
  122.             if(_loc2_.Name == param1)
  123.             {
  124.                return _loc2_;
  125.             }
  126.          }
  127.          trace("Part not found : " + param1);
  128.          return null;
  129.       }
  130.       
  131.       public function getRandomTemplate(param1:String) : PartTemplate
  132.       {
  133.          var _loc2_:PartTemplate = null;
  134.          var _loc3_:int = 0;
  135.          var _loc4_:int = 0;
  136.          var _loc5_:int = 0;
  137.          _loc3_ = 0;
  138.          _loc4_ = 0;
  139.          while(_loc4_ < Templates.length)
  140.          {
  141.             _loc2_ = Templates[_loc4_] as PartTemplate;
  142.             if(_loc2_.Category == param1 || _loc2_.Category == "dress" && param1 == "top")
  143.             {
  144.                _loc3_++;
  145.             }
  146.             _loc4_++;
  147.          }
  148.          _loc5_ = Math.random() * _loc3_ + 1;
  149.          _loc4_ = 0;
  150.          while(_loc4_ < Templates.length)
  151.          {
  152.             _loc2_ = Templates[_loc4_] as PartTemplate;
  153.             if(_loc2_.Category == param1 || _loc2_.Category == "dress" && param1 == "top")
  154.             {
  155.                if(--_loc5_ <= 0)
  156.                {
  157.                   return _loc2_;
  158.                }
  159.             }
  160.             _loc4_++;
  161.          }
  162.          return null;
  163.       }
  164.       
  165.       protected function createBitmapData(param1:BitmapData, param2:int, param3:Array, param4:Number) : BitmapData
  166.       {
  167.          var _loc5_:Rectangle = null;
  168.          var _loc6_:BitmapData = null;
  169.          var _loc7_:int = 0;
  170.          var _loc8_:int = 0;
  171.          _loc5_ = param3[param2] as Rectangle;
  172.          _loc6_ = new BitmapData(_loc5_.width * param4,_loc5_.height * param4,true,0);
  173.          _loc7_ = 0;
  174.          _loc8_ = 1;
  175.          while(_loc8_ < param2)
  176.          {
  177.             _loc7_ += param3[_loc8_].width;
  178.             _loc8_++;
  179.          }
  180.          _loc6_.copyPixels(param1,new Rectangle(_loc7_ * param4,0,_loc5_.width * param4,_loc5_.height * param4),new Point());
  181.          return _loc6_;
  182.       }
  183.       
  184.       public function getFractionDone() : Number
  185.       {
  186.          if(_resmanager.TotalQueued == 0)
  187.          {
  188.             return 0;
  189.          }
  190.          return Number(_resmanager.TotalLoaded) / Number(_resmanager.TotalQueued);
  191.       }
  192.       
  193.       protected function onXMLLoaded() : void
  194.       {
  195.          var xmlRoot:XML = null;
  196.          var sName:* = undefined;
  197.          var sType:* = undefined;
  198.          var sLayer:* = undefined;
  199.          var sX:* = undefined;
  200.          var sWidth:* = undefined;
  201.          var node:XML = null;
  202.          var nodePose:XML = null;
  203.          var nodeRule:XML = null;
  204.          var nodeRuleAttribute:XML = null;
  205.          var nodeAttribute:XML = null;
  206.          var aAttributes:Array = null;
  207.          var sAttributeName:String = null;
  208.          var iAttributeScore:int = 0;
  209.          var partattribute:PartAttribute = null;
  210.          var aBoundaries:Array = null;
  211.          var aPoseAdjustments:Array = null;
  212.          var sFront:String = null;
  213.          var sBack:String = null;
  214.          var sDisplay:String = null;
  215.          var parttemplate:PartTemplate = null;
  216.          var rBounds:Rectangle = null;
  217.          var iPoseNum:int = 0;
  218.          var rule:Rule = null;
  219.          var sRuleAttributeName:String = null;
  220.          var sAttributeCategory:String = null;
  221.          trace("Package " + _sPackageFilename + " config loaded...");
  222.          xmlRoot = _xmlresmanager.getResource("Package",ResourceManager.C_XML);
  223.          for each(node in xmlRoot.elements())
  224.          {
  225.             switch(node.localName())
  226.             {
  227.                case "Item":
  228.                   sName = node.@Name;
  229.                   sType = node.@Type;
  230.                   sLayer = node.@Layer;
  231.                   sFront = node.@Front;
  232.                   sBack = node.@Back;
  233.                   sDisplay = node.@Display;
  234.                   if(isNaN(parseInt(sLayer)))
  235.                   {
  236.                      sLayer = "0";
  237.                      trace("Layer was not specified for " + sName);
  238.                   }
  239.                   aAttributes = new Array();
  240.                   aBoundaries = new Array();
  241.                   aPoseAdjustments = new Array();
  242.                   for each(nodeAttribute in node.elements())
  243.                   {
  244.                      if(nodeAttribute.localName() == "Attribute")
  245.                      {
  246.                         sAttributeName = nodeAttribute.@Name;
  247.                         if(isNaN(parseInt(nodeAttribute.@Score)))
  248.                         {
  249.                            iAttributeScore = 0;
  250.                         }
  251.                         else
  252.                         {
  253.                            iAttributeScore = parseInt(nodeAttribute.@Score);
  254.                         }
  255.                         partattribute = new PartAttribute(sAttributeName,iAttributeScore);
  256.                         aAttributes.push(partattribute);
  257.                      }
  258.                      else if(nodeAttribute.localName() == "bounds")
  259.                      {
  260.                         rBounds = new Rectangle(parseInt(nodeAttribute.@x),parseInt(nodeAttribute.@y),parseInt(nodeAttribute.@width),parseInt(nodeAttribute.@height));
  261.                         iPoseNum = parseInt(nodeAttribute.@pose);
  262.                         while(aBoundaries.length <= iPoseNum)
  263.                         {
  264.                            aBoundaries.push(null);
  265.                            aPoseAdjustments.push(null);
  266.                         }
  267.                         aBoundaries[iPoseNum] = rBounds;
  268.                      }
  269.                   }
  270.                   parttemplate = new PartTemplate(sName,sType,parseInt(sLayer),aAttributes,aBoundaries,aPoseAdjustments);
  271.                   Templates.push(parttemplate);
  272.                   if(sFront != "")
  273.                   {
  274.                      parttemplate.HasFront = true;
  275.                   }
  276.                   if(sBack != "")
  277.                   {
  278.                      parttemplate.HasBack = true;
  279.                   }
  280.                   if(!(sType == "shoe" || sType == "accessory"))
  281.                   {
  282.                      if(sDisplay != "")
  283.                      {
  284.                      }
  285.                   }
  286.                   break;
  287.                case "Poses":
  288.                   for each(nodePose in node.elements())
  289.                   {
  290.                      sName = nodePose.@Name;
  291.                      sX = nodePose.@x;
  292.                      sWidth = nodePose.@width;
  293.                      Poses.push(new Pose(sName,parseInt(sX),parseInt(sWidth)));
  294.                   }
  295.                   break;
  296.                case "Rules":
  297.                   Rules = new Array();
  298.                   for each(nodeRule in node.elements())
  299.                   {
  300.                      rule = new Rule();
  301.                      rule.Name = nodeRule.@name;
  302.                      rule.Comment = nodeRule.@comment;
  303.                      rule.Score = parseInt(nodeRule.@score);
  304.                      rule.Voice = nodeRule.@voice;
  305.                      for each(nodeRuleAttribute in nodeRule.elements())
  306.                      {
  307.                         sRuleAttributeName = nodeRuleAttribute.@name;
  308.                         sAttributeCategory = null;
  309.                         try
  310.                         {
  311.                            sAttributeCategory = nodeRuleAttribute.@category;
  312.                         }
  313.                         catch(exception:Object)
  314.                         {
  315.                         }
  316.                         rule.Attributes.push(new Triple(sRuleAttributeName,sAttributeCategory,null));
  317.                      }
  318.                      Rules.push(rule);
  319.                   }
  320.                   trace("Unknown tag " + node.localName());
  321.                   break;
  322.             }
  323.          }
  324.          _resmanager.loadResource();
  325.       }
  326.       
  327.       public function getTemplateIndexByName(param1:String) : int
  328.       {
  329.          var _loc2_:int = 0;
  330.          _loc2_ = 0;
  331.          while(_loc2_ < Templates.length)
  332.          {
  333.             if(Templates[_loc2_].Name == param1)
  334.             {
  335.                return _loc2_;
  336.             }
  337.             _loc2_++;
  338.          }
  339.          return -1;
  340.       }
  341.       
  342.       protected function onResLoaded() : void
  343.       {
  344.          var mcBitmap:Class = null;
  345.          var parttemplate:PartTemplate = null;
  346.          var bdComposite:BitmapData = null;
  347.          var bdCompositeFront:BitmapData = null;
  348.          var bdCompositeBack:BitmapData = null;
  349.          var pose:Pose = null;
  350.          var iPoseIndex:int = 0;
  351.          var rSource:Rectangle = null;
  352.          var rBounds:Rectangle = null;
  353.          var bitmapdata:BitmapData = null;
  354.          var nScale:Number = NaN;
  355.          var bitmapdataScaled:BitmapData = null;
  356.          var matrix:Matrix = null;
  357.          var pointAdjustment:Point = null;
  358.          var display_name:String = null;
  359.          trace("Package " + _sPackageFilename + " res loaded...");
  360.          for each(parttemplate in Templates)
  361.          {
  362.             try
  363.             {
  364.                mcBitmap = getDefinitionByName(parttemplate.Name) as Class;
  365.                bdComposite = new mcBitmap(0,0);
  366.                bdCompositeFront = null;
  367.                bdCompositeBack = null;
  368.                if(parttemplate.HasFront)
  369.                {
  370.                   mcBitmap = getDefinitionByName(parttemplate.Name + "_front") as Class;
  371.                   bdCompositeFront = new mcBitmap(0,0);
  372.                }
  373.                if(parttemplate.HasBack)
  374.                {
  375.                   mcBitmap = getDefinitionByName(parttemplate.Name + "_back") as Class;
  376.                   bdCompositeBack = new mcBitmap(0,0);
  377.                }
  378.                for each(pose in Poses)
  379.                {
  380.                   iPoseIndex = parseInt(pose.Name);
  381.                   rSource = new Rectangle();
  382.                   rBounds = parttemplate.Boundaries[iPoseIndex] as Rectangle;
  383.                   bitmapdata = createBitmapData(bdComposite,iPoseIndex,parttemplate.Boundaries,BOUNDARY_SCALE);
  384.                   nScale = BOUNDARY_SCALE;
  385.                   matrix = new Matrix();
  386.                   matrix.scale(nScale,nScale);
  387.                   bitmapdataScaled = bitmapdata;
  388.                   parttemplate.setBitmapData(pose.Name,bitmapdataScaled);
  389.                   if(bdCompositeFront != null)
  390.                   {
  391.                      bitmapdata = createBitmapData(bdCompositeFront,iPoseIndex,parttemplate.Boundaries,BOUNDARY_SCALE);
  392.                      bitmapdataScaled = bitmapdata;
  393.                      parttemplate.setBitmapDataFront(pose.Name,bitmapdataScaled);
  394.                   }
  395.                   if(bdCompositeBack != null)
  396.                   {
  397.                      bitmapdata = createBitmapData(bdCompositeBack,iPoseIndex,parttemplate.Boundaries,BOUNDARY_SCALE);
  398.                      bitmapdataScaled = bitmapdata;
  399.                      parttemplate.setBitmapDataBack(pose.Name,bitmapdataScaled);
  400.                   }
  401.                   pointAdjustment = new Point();
  402.                   pointAdjustment.x = (rBounds.x - (pose.x - pose.width / 2)) * nScale;
  403.                   pointAdjustment.y = rBounds.y * nScale;
  404.                   parttemplate.PoseAdjustments[iPoseIndex] = pointAdjustment;
  405.                   if(parttemplate.Category != "shoe" && pose.Name == ModelPose.DEFAULT_SOURCE || parttemplate.Category == "shoe" && pose.Name == ModelPose.DEFAULT_SHOE_SOURCE)
  406.                   {
  407.                      if(parttemplate.Category == "body")
  408.                      {
  409.                         nScale = 0.2 / BOUNDARY_SCALE;
  410.                      }
  411.                      else if(parttemplate.Category == "dress")
  412.                      {
  413.                         nScale = 0.3 / BOUNDARY_SCALE;
  414.                      }
  415.                      else
  416.                      {
  417.                         nScale = 0.5 / BOUNDARY_SCALE;
  418.                      }
  419.                      bitmapdataScaled = new BitmapData(bitmapdata.width * nScale,bitmapdata.height * nScale,true,0);
  420.                      matrix = new Matrix();
  421.                      matrix.scale(nScale,nScale);
  422.                      bitmapdataScaled.draw(bitmapdata,matrix,null,null,null,true);
  423.                      parttemplate.setBitmapData(ModelPose.DEFAULT,bitmapdataScaled);
  424.                   }
  425.                }
  426.                if(bdCompositeFront != null)
  427.                {
  428.                   bdCompositeFront.dispose();
  429.                   bdCompositeFront = null;
  430.                }
  431.                if(bdCompositeBack != null)
  432.                {
  433.                   bdCompositeBack.dispose();
  434.                   bdCompositeBack = null;
  435.                }
  436.                bdComposite.dispose();
  437.                bdComposite = null;
  438.             }
  439.             catch(ex:Object)
  440.             {
  441.                trace("*****************Error loading: " + parttemplate.Name);
  442.             }
  443.             try
  444.             {
  445.                display_name = parttemplate.Name + "_display";
  446.                if(getDefinitionByName(display_name) != null)
  447.                {
  448.                   mcBitmap = getDefinitionByName(display_name) as Class;
  449.                   bitmapdata = new mcBitmap(0,0);
  450.                   if(bitmapdata.height < SHOE_SCALE_CUTTOFF)
  451.                   {
  452.                      nScale = 1;
  453.                   }
  454.                   else
  455.                   {
  456.                      nScale = 0.5;
  457.                   }
  458.                   bitmapdataScaled = new BitmapData(bitmapdata.width * nScale,bitmapdata.height * nScale,true,0);
  459.                   matrix = new Matrix();
  460.                   matrix.scale(nScale,nScale);
  461.                   bitmapdataScaled.draw(bitmapdata,matrix,null,null,null,true);
  462.                   parttemplate.setBitmapData(ModelPose.DEFAULT,bitmapdataScaled);
  463.                   parttemplate.DefaultBitmapProvided = true;
  464.                   bitmapdata.dispose();
  465.                   bitmapdata = null;
  466.                }
  467.             }
  468.             catch(ex:Object)
  469.             {
  470.                trace("Error parsing bitmap " + parttemplate.Name + "\n" + ex.toString());
  471.             }
  472.          }
  473.          _xmlresmanager.purgeAllResources();
  474.          _resmanager.purgeAllResources();
  475.          _fnCallback(_sPackageFilename);
  476.       }
  477.       
  478.       public function loadData(param1:String, param2:Function) : void
  479.       {
  480.          _sPackageFilename = param1;
  481.          _fnCallback = param2;
  482.          _xmlresmanager.CallbackFunction = onXMLLoaded;
  483.          _resmanager.CallbackFunction = onResLoaded;
  484.          _xmlresmanager.setXMLResource("Package",param1);
  485.          onXMLLoaded();
  486.       }
  487.       
  488.       public function copy(param1:PartTemplatePackage) : void
  489.       {
  490.          var _loc2_:PartTemplate = null;
  491.          for each(_loc2_ in param1.Templates)
  492.          {
  493.             Templates.push(_loc2_);
  494.          }
  495.          Poses = param1.Poses;
  496.          Rules = param1.Rules;
  497.       }
  498.       
  499.       public function filter(param1:Array) : void
  500.       {
  501.          var _loc2_:Array = null;
  502.          var _loc3_:PartTemplate = null;
  503.          var _loc4_:String = null;
  504.          _loc2_ = new Array();
  505.          for each(_loc3_ in Templates)
  506.          {
  507.             if(_loc3_.Layer == 0)
  508.             {
  509.                _loc2_.push(_loc3_);
  510.             }
  511.             else
  512.             {
  513.                for each(_loc4_ in param1)
  514.                {
  515.                   if(_loc3_.Name == _loc4_)
  516.                   {
  517.                      _loc2_.push(_loc3_);
  518.                      break;
  519.                   }
  520.                }
  521.             }
  522.          }
  523.          Templates = _loc2_;
  524.       }
  525.       
  526.       protected function trimBitmapData(param1:BitmapData, param2:Boolean = false) : BitmapData
  527.       {
  528.          var _loc3_:BitmapData = null;
  529.          var _loc4_:Rectangle = null;
  530.          var _loc5_:Rectangle = null;
  531.          var _loc6_:Point = null;
  532.          var _loc7_:int = 0;
  533.          var _loc8_:int = 0;
  534.          _loc4_ = new Rectangle();
  535.          _loc5_ = new Rectangle();
  536.          _loc6_ = new Point();
  537.          _loc4_.x = 0;
  538.          _loc4_.y = 0;
  539.          _loc4_.width = param1.width;
  540.          _loc4_.height = 1;
  541.          if(param2)
  542.          {
  543.             _loc7_ = 0;
  544.             while(_loc7_ < param1.height)
  545.             {
  546.                _loc4_.y = _loc7_;
  547.                if(param1.hitTest(_loc6_,10,_loc4_))
  548.                {
  549.                   _loc5_.y = _loc7_;
  550.                   break;
  551.                }
  552.                _loc7_++;
  553.             }
  554.          }
  555.          _loc7_ = param1.height - 1;
  556.          while(_loc7_ >= 0)
  557.          {
  558.             _loc4_.y = _loc7_;
  559.             if(param1.hitTest(_loc6_,10,_loc4_))
  560.             {
  561.                _loc5_.height = _loc7_ - _loc5_.y;
  562.                break;
  563.             }
  564.             _loc7_--;
  565.          }
  566.          _loc4_.x = 0;
  567.          _loc4_.y = 0;
  568.          _loc4_.width = 1;
  569.          _loc4_.height = param1.height;
  570.          if(param2)
  571.          {
  572.             _loc7_ = 0;
  573.             while(_loc7_ < param1.width)
  574.             {
  575.                _loc4_.x = _loc7_;
  576.                if(param1.hitTest(_loc6_,10,_loc4_))
  577.                {
  578.                   _loc5_.x = _loc7_;
  579.                   break;
  580.                }
  581.                _loc7_++;
  582.             }
  583.          }
  584.          _loc7_ = param1.width - 1;
  585.          while(_loc7_ >= 0)
  586.          {
  587.             _loc4_.x = _loc7_;
  588.             if(param1.hitTest(_loc6_,10,_loc4_))
  589.             {
  590.                _loc5_.width = _loc7_ - _loc5_.x;
  591.                break;
  592.             }
  593.             _loc7_--;
  594.          }
  595.          _loc3_ = new BitmapData(_loc5_.width,_loc5_.height);
  596.          _loc3_.copyPixels(param1,_loc5_,new Point());
  597.          return _loc3_;
  598.       }
  599.    }
  600. }
  601.